package com.study.www.config;

import com.study.www.excception.ParamException;
import com.study.www.excception.UrlException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 异常处理
 * @author : Administrator.zhuyanpeng
 * @date : 2019/11/12 - 11:20
 */
@Slf4j
@ControllerAdvice
public class ExceptionHandle {

    /**
     * 异常拦截返回的
     * @param
     * @return
     * @throws
     * @author zhuyanpeng
     * @date 2019/11/12
     */
    @ExceptionHandler(ParamException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    @ResponseBody
    public Result bindException(ParamException  e){
        log.info(" ============= ParamException  =============");
        return  Result.error(e.getCode(),e.getMessage());
    }


    /**
     * 跳转到异常界面的
     * @param
     * @return
     * @throws
     * @author zhuyanpeng
     * @date 2019/11/12
     */
    @ExceptionHandler(UrlException.class)
    public ModelAndView bindException(HttpServletRequest request,UrlException  e){
        log.info(" ============= UrlException  =============");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message",e.getMessage());
        modelAndView.addObject("url",request.getRequestURI());
        modelAndView.setViewName(e.getUrl());
        return  modelAndView;
    }

}
